import xbmc
import xbmcgui
import urllib, urllib2
import os, sys, re

__cwd__      = sys.modules[ "__main__" ].__cwd__
__settings__ = sys.modules[ "__main__" ].__settings__
__htm__      = sys.modules[ "__main__" ].__htm__
__fav__      = sys.modules[ "__main__" ].__fav__
__test__     = sys.modules[ "__main__" ].__test__
__vdata__    = sys.modules[ "__main__" ].__vdata__

class GUI(xbmcgui.WindowXML):

	def __init__(self, *args, **kwargs):
		xbmcgui.WindowXMLDialog.__init__(self, *args, **kwargs)
		self.doModal()

	def onInit(self):
		self.testmode = __settings__.getSetting('testmode')
		self.action_cancel_dialog = (9, 10)
		self.list = self.getControl(62)
		self.list.reset()
		# ===================================================
		# Add list items here :
		for fav in __fav__.List:
			print 'Fav [%s] : [%s]' % (fav, __fav__.List[ fav ])
			if (self.testmode != 'true'):
				img = 'http://i.vplay.ro/ch/' + fav + '.jpg'
			else:
				img = ''
			item = xbmcgui.ListItem(__fav__.List[ fav ], __fav__.List[ fav ], img, img, fav )
			item.setProperty('id', fav)
			self.list.addItem(item)
		
		self.getControl(57).setImage(__htm__.UserAvatar)
		self.getControl(58).setLabel(__htm__.UserID)
		self.getControl(59).setLabel(__htm__.UserDays + ' zile VIP')
			
		# ===================================================

	def onClick(self, controlId):
		if controlId == 9001:
			print 'Main'
		elif controlId == 9003:
			import resources.lib.GenreList as GenreList
			page_genre_list = GenreList.GUI("vplay_genre_list.xml", __cwd__, "default")
			
		elif controlId == 9004:
			keyboard = xbmc.Keyboard('', 'Enter keyword:', False)
			kw = ''
			canceled = 'false'
			while (kw=='' and canceled=='false') :
				keyboard.doModal()
				if (keyboard.isConfirmed()):
					kw = keyboard.getText()
				if len(kw)==0:
					canceled='true'
			if canceled=='false':
				print 'Keyword : [%s]' % kw
				import resources.lib.Search as Search
				page_search = Search.GUI("vplay_search.xml", __cwd__, "default", keyword=kw)
				
		elif controlId == 9005:
			print 'Login'
			__settings__.openSettings()

		elif controlId == 62:
			item = self.list.getSelectedItem()
			print 'Item : [%s]' % item.getLabel()
			print 'ID   : [%s]' % item.getProperty('id')
			import resources.lib.Serie as Serie
			page_serie = Serie.GUI("vplay_serie.xml", __cwd__, "default", id = item.getProperty('id'))

	def onAction(self, action):
		if action in self.action_cancel_dialog:
			self.close()

	def onFocus(self, controlId):
		pass
